package com.sun.showcase.biz.basic.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.basic.dao.TenantDao;
import com.sun.showcase.biz.basic.service.TenantService;
import com.sun.showcase.client.domain.basic.Tenant;
import com.sun.showcase.client.query.basic.TenantQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("tenantService")
@Transactional
public class TenantServiceImpl implements TenantService{
	@Autowired
	private TenantDao tenantDao;
	
	public void setTenantDao(TenantDao dao) {
		this.tenantDao = dao;
	}

	public DataGrid datagrid(TenantQuery tenantQuery) {
		DataGrid j = new DataGrid();
		Pager<Tenant> pager  = find(tenantQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<TenantQuery> getQuerysFromEntitys(List<Tenant> tenants) {
		List<TenantQuery> tenantQuerys = new ArrayList<TenantQuery>();
		if (tenants != null && tenants.size() > 0) {
			for (Tenant tb : tenants) {
				TenantQuery b = new TenantQuery();
				BeanUtils.copyProperties(tb, b);
				tenantQuerys.add(b);
			}
		}
		return tenantQuerys;
	}

	private Pager<Tenant> find(TenantQuery tenantQuery) {
		Page<Tenant> page = PageHelper.startPage(tenantQuery.getPage().intValue(), tenantQuery.getRows().intValue());
		List<Tenant> list = tenantDao.findList(tenantQuery);
		Pager<Tenant> pager = new Pager<Tenant>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Tenant add(TenantQuery tenantQuery) {
		Tenant t = new Tenant();
		BeanUtils.copyProperties(tenantQuery, t);
		tenantDao.save(t);
		tenantQuery.setId(t.getId());
		return t;
		
	}

	public void update(TenantQuery tenantQuery) {
		Tenant t = tenantDao.getById(tenantQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(tenantQuery, t);
	    	tenantDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(TenantQuery tenantQuery) {
	    tenantDao.updatePart(tenantQuery);
	}

	public void delete(Long[] ids) {
		tenantDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(Long[] ids) {
		tenantDao.deletePt(ids);
	}

	public Tenant get(TenantQuery tenantQuery) {
		return tenantDao.getById((tenantQuery.getId()));
	}

	public Tenant get(Long id) {
		return tenantDao.getById(id);
	}

	
	public List<TenantQuery> listAll(TenantQuery tenantQuery) {
	    List<Tenant> list = tenantDao.findList(tenantQuery);
		List<TenantQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	private Cache<String,Tenant> cache = CacheUtil.newFIFOCache(200);
	@Override
	public Tenant getByTenantCode(String tenantCode) {
		if(cache.containsKey(tenantCode)){
			return cache.get(tenantCode);
		}
		Tenant t =  tenantDao.getByTenantCode(tenantCode);
		if(t != null){
			cache.put(tenantCode,t);
		}
		return t;
	}
}
